package main.java.com.epic.web.util;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class HttpHelper {

    public static final String LOGGER_RESULT = "loggerResult";
    public static final String LOGGER_CONTENT = "loggerContent";
    public static final String LOGGER_TYPE = "loggerType";
    public static final String TARGET_ID = "targetId";
    public static final String TARGET_TYPE = "targetType";
    public static final String ATTR_REQUEST_ID = "requestId";

    public static final String ATTR_ABSTRACT_ACCOUNT = "ATTR_ABSTRACT_ACCOUNT";

    public static final String USER_GROUP = "USER_GROUP";
    public static final String TENANT_IDS = "TENANT_IDS";
    public static final String GROUP_IDS = "GROUP_IDS";


    public static String getParameter(HttpServletRequest request, String param){
        if (request == null) {
            return "";
        }
        return request.getParameter(param);
    }

    public static String getMethod(HttpServletRequest request) {
        if (request == null) {
            return "";
        }
        return request.getMethod();
    }
    public static String getRequestURI(HttpServletRequest request) {
        if (request == null) {
            return "";
        }
        return request.getRequestURI();
    }

    public final static void setRequestId(HttpServletRequest request, long id) {
        if (request != null) {
            request.setAttribute(ATTR_REQUEST_ID, id);
        }
    }
    public static Long getRequestId(HttpServletRequest request) {
        if (request == null) {
            return 0L;
        }
        Object requestId = request.getAttribute("requestId");
        if(requestId == null){
            return 0L;
        }
        return (Long)requestId ;
    }
    public final static void setLogger(HttpServletRequest request, String type, String content) {

        setLogger(request, type, content, null);

    }

    public final static void setLogger(HttpServletRequest request, String type, String content, String result, String targetType, String targetId) {

        if (request != null) {
            request.setAttribute(LOGGER_CONTENT, content);
            request.setAttribute(LOGGER_TYPE, type);
            request.setAttribute(LOGGER_RESULT, result);
            request.setAttribute(TARGET_TYPE, targetType);
            request.setAttribute(TARGET_ID, targetId);
        }

    }
    public final static void setLogger(HttpServletRequest request, String type, String content, String result) {
        setLogger(request, type, content, result, null, null);
    }

    public final static String getLoggerType(HttpServletRequest request) {
        String loggerType = null;
        if (request != null) {
            loggerType = (String) request.getAttribute(LOGGER_TYPE);
        }

        return loggerType;

    }
    public final static void setLoggerResult(HttpServletRequest request, String result) {
        if (request != null) {
            request.setAttribute(HttpHelper.LOGGER_RESULT, result);
        }
    }

    public final static String getLoggerResult(HttpServletRequest request) {
        String loggerResult = null;
        if (request != null) {
            loggerResult = (String) request.getAttribute(LOGGER_RESULT);
        }

        return loggerResult;

    }

    public final static String getLoggerTargetId(HttpServletRequest request) {
        String targetId = null;
        if (request != null) {
            targetId = (String) request.getAttribute(TARGET_ID);
        }

        return targetId;

    }


    public final static String getLoggerTargetType(HttpServletRequest request) {
        String targetType = null;
        if (request != null) {
            targetType = (String) request.getAttribute(TARGET_TYPE);
        }

        return targetType;

    }


    public final static String getLoggerContent(HttpServletRequest request) {
        String loggerContent = null;
        if (request != null) {
            loggerContent = (String) request.getAttribute(LOGGER_CONTENT);
        }

        return loggerContent;

    }
    public final static Integer getAccountId(HttpServletRequest request) {
        Integer accountId = null;
        if (request != null) {
            String idStr = request.getParameter("accountId");
            // StringUtils 来自https://commons.apache.org/proper/commons-lang/
            if (!StringUtils.isEmpty(idStr)) {
                accountId = new Integer(idStr);
            } else {
                idStr = (String) request.getAttribute("accountId");
                if (!StringUtils.isEmpty(idStr)) {
                    accountId = new Integer(idStr);
                }
            }
        }

        return accountId;

    }

    public final static String getIpAddress(HttpServletRequest request) {
        if (request == null) {
            try {
                return InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException e) {
                return "";
            }
        }
        // 获取请求主机IP地址,如果通过代理进来，则透过防火墙获取真实IP地址
        String ip = request.getHeader("X-Forwarded-For");

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");

            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");

            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");

            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");

            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();

            }
        } else if (ip.length() > 15) {
            String[] ips = ip.split(",");
            for (int index = 0; index < ips.length; index++) {
                String strIp = (String) ips[index];
                if (!("unknown".equalsIgnoreCase(strIp))) {
                    ip = strIp;
                    break;
                }
            }
        }
        return ip;
    }

}
